package com.ybkj.daijia.api.driver.v3;

import com.ybkj.daijia.api.driver.ErrorConstant;
import com.ybkj.daijia.api.driver.SecurityUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Notice;
import com.ybkj.daijia.server.model.NoticeXDriver;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.NoticeService;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("driverNoticeV3Controller")
@RequestMapping(value = "driver/api/rest/v3")
public class NoticeController {

    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private DriverService driverService;


    /**
     * 查询通知信息
     *
     * @param driverID
     * @param page
     * @param limit
     * @param appKey
     * @param token
     * @param timestamp
     * @return
     */
    @RequestMapping("queryNotice")
    public @ResponseBody
    ResultTransport queryNotice(Long driverId, Integer page, Integer limit, String appKey,
        String token, String timestamp) {

        logger.debug("recv driverId:{},page:{},limit:{},appKey:{},token:{},timestamp:{}", driverId,
            page, limit, appKey, token, timestamp);

        if (null == driverId || null == page || null == limit || SecurityUtils
            .isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        if (!StringUtils.isNumeric(timestamp)
            || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
            return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(driverId));
        params.add(String.valueOf(page));
        params.add(String.valueOf(limit));
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        try {
            Driver driver = driverService.findOne(driverId);

            if (null == driver) {
                return ResultTransport.getErrorBy(ErrorConstant.DRIVER_NOT_EXISTS);
            }

            Page<Map<String, Object>> notice = noticeService.queryNoticeV3(driverId, page, limit);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(notice);

            return instance;

        } catch (Exception e) {
            logger.error("api v1 driver queryNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

    @RequestMapping(value = "readNotice", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport readNotice(Long driverId, Long noticeId, String appKey, String token,
        String timestamp) {

        logger.debug("recv driverId:{},noticeId:{},appKey:{},token:{},timestamp:{}", driverId,
            noticeId, appKey, token, timestamp);

        if (null == driverId || null == noticeId || SecurityUtils
            .isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        if (!StringUtils.isNumeric(timestamp) || !SecurityUtils
            .checkTimestamp(Long.parseLong(timestamp))) {
            return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(driverId));
        params.add(String.valueOf(noticeId));
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        try {
            NoticeXDriver noticeXDriver = noticeService.findByDriverAndNotice(driverId, noticeId);

            if (null != noticeXDriver) {
                noticeService.updateHasRead(noticeXDriver.getId());
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);

            return instance;

        } catch (Exception e) {
            logger.error("api v3 driver readNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }
    }

    /**
     * 查询通知信息
     *
     * @param driverID
     * @param page
     * @param limit
     * @param appKey
     * @param token
     * @param timestamp
     * @return
     */
    @RequestMapping("queryOneNotice")
    public @ResponseBody
    ResultTransport queryOneNotice(Long id, String appKey, String token, String timestamp) {

        logger.debug("recv id:{},appKey:{},token:{},timestamp:{}", id, appKey, token, timestamp);

        if (null == id || SecurityUtils.isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        if (!StringUtils.isNumeric(timestamp)
            || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
            return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(id));
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        try {

            Notice notice = noticeService.findOne(id);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(notice);

            return instance;

        } catch (Exception e) {
            logger.error("api v1 driver queryOneNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

    /**
     * 查询通知信息
     */
    @RequestMapping("viewNotice/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView mv = new ModelAndView();

        Notice notice = noticeService.findOne(id);

        mv.addObject("notice", notice);

        mv.setViewName("notice/view");

        return mv;
    }
}
